package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.entity.BusinessModel;
import com.rules.admin.entity.RulePackage;
import com.rules.admin.service.BusinessModelService;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping({"/businessModel"})
@Controller
public class BusinessModelController {
   @Autowired
   private BusinessModelService businessModelService;
   @Autowired
   private RulePackageService rulePackageService;

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<BusinessModel> queryPage(String name, IPage<BusinessModel> page) {
      return this.businessModelService.queryPage(name, page);
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addBusinessModel(@RequestBody BusinessModel businessModel) {
      if (this.businessModelService.findByName(businessModel.getName()) != null) {
         return ResultUtils.getFaildResultData(new String[]{"name不能重复"});
      } else {
         this.businessModelService.save(businessModel);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public BusinessModel getBusinessModel(@PathVariable("id") Long id) {
      return this.businessModelService.getById(id);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateBusinessModel(@RequestBody BusinessModel businessModel) {
      BusinessModel model = this.businessModelService.findByName(businessModel.getName());
      if (model != null && !model.getId().equals(businessModel.getId())) {
         return ResultUtils.getFaildResultData(new String[]{"name不能重复"});
      } else {
         this.businessModelService.update(businessModel);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteBusinessModel(@PathVariable("id") Long id) {
      List<RulePackage> rulePackages = this.rulePackageService.getRulePackagesByBusinessModel(id);
      if (rulePackages != null && !rulePackages.isEmpty()) {
         return ResultUtils.getFaildResultData(new String[]{"该模型已被规则包引用，请先移除对应的规则包引用"});
      } else {
         this.businessModelService.removeById(id);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/id"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public BusinessModel addBusinessModelReturnId(@RequestBody BusinessModel businessModel) {
      this.businessModelService.save(businessModel);
      return businessModel;
   }
}
